﻿Imports System.IO
Imports SecretaryToolData
Imports iTextSharp.text
Imports iTextSharp.text.pdf

Partial Public Class Reports

	Public Shared Function GenerateUpdateCardsReport(Optional ShowUnsubmittedTime As Boolean = False) As FileInfo
		Dim myUpdateCardsReportData As New VM_UpdateCards(ShowUnsubmittedTime)

		Dim reportMonth As String = If(ReportLastSubmitted > Date.MinValue, ReportLastSubmitted.AddMonths(-1).ToString("MMM yyyy"), "Unknown")
		Dim OutputFile As New FileInfo(String.Format("{0}\{1}.{2}", ConfigPropertyKeys.AutoBackupLocation, String.Format("Update Cards for {0}", reportMonth).AppendFileDateExtension, "pdf"))

		Using myFileStream As New FileStream(OutputFile.FullName, FileMode.Create, FileAccess.Write)
			Using myDoc As New Document(PageSize.LETTER)
				Dim writer As PdfWriter = PdfWriter.GetInstance(myDoc, myFileStream)

				myDoc.Open()
				Dim content As PdfContentByte = writer.DirectContent

				For Each publisher In myUpdateCardsReportData.PublisherReports
					WritePublisherCardRecordRow(myDoc, publisher)
				Next

				With myUpdateCardsReportData.PublisherReports
					WritePublisherCardRecordRow(myDoc, "Publisher Totals", reportMonth, .Sum(Function(x) x.Books), .Sum(Function(x) x.Brochures), .Sum(Function(x) x.Hours), .Sum(Function(x) x.Magazines), .Sum(Function(x) x.ReturnVisits), .Sum(Function(x) x.BibleStudies), String.Format("Count: {0}", .Count))
				End With

				For Each auxPioneer In myUpdateCardsReportData.AuxPioneerReports
					WritePublisherCardRecordRow(myDoc, auxPioneer)
				Next

				With myUpdateCardsReportData.AuxPioneerReports
					WritePublisherCardRecordRow(myDoc, "Aux Pioneer Totals", reportMonth, .Sum(Function(x) x.Books), .Sum(Function(x) x.Brochures), .Sum(Function(x) x.Hours), .Sum(Function(x) x.Magazines), .Sum(Function(x) x.ReturnVisits), .Sum(Function(x) x.BibleStudies), String.Format("Count: {0}", .Count))
				End With

				For Each pioneer In myUpdateCardsReportData.PioneerReports
					WritePublisherCardRecordRow(myDoc, pioneer)
				Next

				With myUpdateCardsReportData.PioneerReports
					WritePublisherCardRecordRow(myDoc, "Pioneer Totals", reportMonth, .Sum(Function(x) x.Books), .Sum(Function(x) x.Brochures), .Sum(Function(x) x.Hours), .Sum(Function(x) x.Magazines), .Sum(Function(x) x.ReturnVisits), .Sum(Function(x) x.BibleStudies), String.Format("Count: {0}", .Count))
				End With

				For Each specialPioneer In myUpdateCardsReportData.SpecialPioneerReports
					WritePublisherCardRecordRow(myDoc, specialPioneer)
				Next

				With myUpdateCardsReportData.SpecialPioneerReports
					WritePublisherCardRecordRow(myDoc, "Special Pioneer Totals", reportMonth, .Sum(Function(x) x.Books), .Sum(Function(x) x.Brochures), .Sum(Function(x) x.Hours), .Sum(Function(x) x.Magazines), .Sum(Function(x) x.ReturnVisits), .Sum(Function(x) x.BibleStudies), String.Format("Count: {0}", .Count))
				End With

			End Using
		End Using

		Return OutputFile

	End Function

	Private Shared Sub WritePublisherCardRecordRow(ByRef myDoc As Document, ByRef publisherRecord As VM_PublisherCardRecord)
		With publisherRecord
			Dim newRemarks As String = .Remarks
			Dim publisherData As New VM_PublisherCard(.PublisherID)
			If publisherRecord.IsPioneer AndAlso publisherRecord.ReportMonth.Month = PreviousReportLastSubmitted.Month Then
				newRemarks &= String.Format(" (Pace: {0})", publisherData.PioneerHoursPaceThisServiceYear)
			End If
			If publisherRecord.IsPioneer AndAlso publisherRecord.PioneerCreditHours > 0 Then
				newRemarks &= String.Format(" (Pioneer Credit Hours: {0})", publisherRecord.PioneerCreditHours)
			End If
			WritePublisherCardRecordRow(myDoc, publisherData.PublisherName, .ReportMonth, .Books, .Brochures, .Hours, .Magazines, .ReturnVisits, .BibleStudies, newRemarks)
		End With
	End Sub

	Private Shared Sub WritePublisherCardRecordRow(ByRef myDoc As Document, FullName As String, ReportMonth As Date, Books As Integer, Brochures As Integer, Hours As Integer, Magazines As Integer, ReturnVisits As Integer, BibleStudies As Integer, Remarks As String)
		Dim table As New PdfPTable(8)
		table.KeepTogether = True
		table.WidthPercentage = 100

		Dim tableTitleCell As New PdfPCell(New Phrase(FullName, fontSectionLabel))
		tableTitleCell.Colspan = 8
		table.AddCell(tableTitleCell)

		table.AddCell(PrettyCell("Month"))
		table.AddCell(PrettyCell("Bks."))
		table.AddCell(PrettyCell("Broch."))
		table.AddCell(PrettyCell("Hrs."))
		table.AddCell(PrettyCell("Mag."))
		table.AddCell(PrettyCell("RVs"))
		table.AddCell(PrettyCell("Studies"))
		table.AddCell(PrettyCell("Remarks"))

		table.AddCell(PrettyCell(ReportMonth.ToString("MMM yyyy")))
		table.AddCell(PrettyCell(Books))
		table.AddCell(PrettyCell(Brochures))
		table.AddCell(PrettyCell(Hours))
		table.AddCell(PrettyCell(Magazines))
		table.AddCell(PrettyCell(ReturnVisits))
		table.AddCell(PrettyCell(BibleStudies))
		table.AddCell(PrettyCell(Remarks))

		myDoc.Add(table)

		myDoc.Add(New Paragraph(" "))

	End Sub
End Class
